"""
Compliance Template 相关的请求和响应模型
"""
from datetime import datetime
from typing import Optional, List
from uuid import UUID
from pydantic import BaseModel, Field, field_validator

# TemplateItem 相关模型
class TemplateItemBase(BaseModel):
    """
    Template Item 基本模型，包含共用属性
    """
    title: str = Field(..., description="标题")
    description: Optional[str] = Field(None, description="描述")
    guide: Optional[str] = Field(None, description="指导说明")
    compliance_type: str = Field(..., description="合规类型", pattern="^(risk|guarantee)$")
    maturity_level: Optional[int] = Field(1, description="成熟度等级", ge=1, le=5)

class TemplateItemCreate(TemplateItemBase):
    """
    创建 Template Item 的请求模型
    """
    template_id: Optional[UUID] = Field(None, description="关联的模板ID")

class TemplateItemResponse(TemplateItemBase):
    """
    Template Item 响应模型
    """
    id: UUID = Field(..., description="唯一标识符")
    template_id: UUID = Field(..., description="关联的模板ID")

    class Config:
        """Pydantic配置类"""
        from_attributes = True

# ComplianceTemplate 相关模型
class ComplianceTemplateBase(BaseModel):
    """
    Compliance Template 基本模型，包含共用属性
    """
    name: str = Field(..., description="模板名称")
    description: Optional[str] = Field(None, description="模板描述")
    assessment_type: str = Field(..., description="模板评估类型", pattern="^(101|102|103|104|105)$")
    version: str = Field("1.0.0", description="版本号", pattern="^\\d+\\.\\d+\\.\\d+$")
    default_maturity_level: Optional[int] = Field(1, description="默认成熟度等级", ge=1, le=5)
class ComplianceTemplateCreate(ComplianceTemplateBase):
    """
    创建 Compliance Template 的请求模型
    """
    items: Optional[List[TemplateItemCreate]] = Field(None, description="模板项列表")

class ComplianceTemplateUpdate(BaseModel):
    """
    更新 Compliance Template 的请求模型，所有字段都是可选的
    """
    name: Optional[str] = Field(None, description="模板名称")
    description: Optional[str] = Field(None, description="模板描述")
    assessment_type: Optional[str] = Field(None, description="模板评估类型", pattern="^(101|102|103|104|105)$")
    version: Optional[str] = Field(None, description="版本号", pattern="^\\d+\\.\\d+\\.\\d+$")
    default_maturity_level: Optional[int] = Field(None, description="默认成熟度等级", ge=1, le=5)
    @field_validator('name')
    @classmethod
    def validate_name_not_empty(cls, v):
        """验证名称不为空"""
        if v == "":
            raise ValueError('模板名称不能为空')
        return v

class ComplianceTemplateResponse(ComplianceTemplateBase):
    """
    Compliance Template 响应模型，包含所有字段
    """
    id: UUID = Field(..., description="唯一标识符")
    created_at: datetime = Field(..., description="创建时间")
    updated_at: datetime = Field(..., description="更新时间")

    class Config:
        """Pydantic配置类"""
        from_attributes = True

class ComplianceTemplateDetailResponse(ComplianceTemplateResponse):
    """
    包含模板项的完整 Compliance Template 响应模型
    """
    items: List[TemplateItemResponse] = Field([], description="模板项列表")

    class Config:
        """Pydantic配置类"""
        from_attributes = True

class ComplianceTemplatePagination(BaseModel):
    """
    分页查询响应模型
    """
    total: int = Field(..., description="总记录数")
    items: List[ComplianceTemplateResponse] = Field(..., description="当前页数据")
